﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using ElectronicCommerce.Framework.ServiceModel.WCF;

namespace ElectronicCommerce.Framework.ServiceModel
{
    /// <summary>
    /// 
    /// </summary>
    public class ServiceBroker
    {
        /// <summary>
        /// 根据配置文件装载相应的Service。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetService<T>() where T : class
        {
            IServiceBroker serviceBroker = ServiceBrokerFactory.Create(typeof(T));
            return serviceBroker.GetService<T>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public static T GetService<T>(string serviceName) where T : class
        {
            IServiceBroker serviceBroker = ServiceBrokerFactory.Create(typeof(T), serviceName);
            return serviceBroker.GetService<T>(serviceName);
        }

        /// <summary>
        /// 根据配置文件装载相应的Service。
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        public static object GetService(Type serviceType, string serviceName)
        {
            IServiceBroker serviceBroker = ServiceBrokerFactory.Create(serviceType, serviceName);
            return serviceBroker.GetService(serviceType, serviceName);
        }

        /// <summary>
        /// 关闭与服务器的连接。
        /// </summary>
        /// <param name="serviceClient"></param>
        public static void Close(object serviceClient)
        {
            ICommunicationObject o = serviceClient as ICommunicationObject;
            if (o != null)
            {
                WCFUtility.CloseConnection(o);
            }
        }
    }
}
